package com.example.weatherdemo.slice;

import com.example.weatherdemo.ResourceTable;
import ohos.agp.components.*;
import ohos.app.Context;

import java.util.List;

public class WeatherListDataProvider extends BaseItemProvider {
    private List<WeatherItem> list;
    private Context context;

    public WeatherListDataProvider(Context context, List<WeatherItem> data) {
        this.list = data;
        this.context = context;
    }


    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public Component getComponent(int i, Component convertComponent, ComponentContainer componentContainer) {
        final Component cpt;
        if (convertComponent == null) {
            cpt = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_list_item, null, false);
        } else {
            cpt = convertComponent;
        }
        WeatherItem sampleItem = list.get(i);
        Text t1 = cpt.findComponentById(ResourceTable.Id_date);
        Text t2 = cpt.findComponentById(ResourceTable.Id_temperature);
        Text t3 = cpt.findComponentById(ResourceTable.Id_direct);
        Text t4 = cpt.findComponentById(ResourceTable.Id_weather);
        t1.setText(sampleItem.getDate());
        t2.setText(sampleItem.getTemperature());
        t3.setText(sampleItem.getDirect());
        t4.setText(sampleItem.getWeather());
        return cpt;
    }
}
